import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import store from '@/store'
// import GoodsList from '../views/GoodsList/GoodsList.vue'
const GoodsList=()=>import("../views/GoodsList/GoodsList.vue")
const Login=()=>import("../views/Login/Login.vue")
const Register=()=>import("../views/Register/Register.vue")
const Cart=()=>import("../views/Cart/Cart.vue")
const Order=()=>import("../views/Order/Order.vue")
const Profile=()=>import("../views/Profile/Profile.vue")

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
	meta:{
		title:'京西商城首页'
	}
  },
  {
    path: '/goods/:keyword/:page',
    name: 'GoodsList',
    component: GoodsList,
  	meta:{
  		title:'商品列表页',
		// isAuthRequired:true
  	}
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
  	meta:{
  		title:'京西商城登录'
  	}
  },
  {
    path: '/register',
    name: 'register',
    component: Register,
  	meta:{
  		title:'京西商城注册'
  	}
  },
  {
    path: '/cart/detail',
    name: 'Cart',
    component: Cart,
  	meta:{
  		title:'购物车',
  		// isAuthRequired:true
  	}
  },
  {
    path: '/order/:tradeNo',
    name: 'Order',
    component: Order,
  	meta:{
  		title:'订单页面',
  		// isAuthRequired:true
  	}
  },
  {
    path: '/profile/',
    name: 'Profile',
    component: Profile,
  	meta:{
  		title:'您的订单',
  		// isAuthRequired:true
  	}
  },
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to,from)=>{
	document.title = to.meta.title;
	// 导航守卫做的权限验证，是对于前端来说的
	if(to.meta.isAuthRequired==true && store.state.user.isLogin==false){
		if(window.localStorage.getItem("token"))
		router.push("/login")
	}
})

export default router